package com.example.wly.application_3_homework;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.HashMap;
import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;
import pub.devrel.easypermissions.EasyPermissions.PermissionCallbacks;

public class MainActivity extends AppCompatActivity implements PermissionCallbacks {
    public static final String TAG="Main4";
    private EditText etNumber;
    HashMap<String, String> phoneMap = new HashMap<>();
    private EditText etNumber2;
    private EditText etPhone;
    public String cutString;
    public  String phoneNumber;

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        // Forward results to EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //权限申请
        requestMyAppPermission();
        //日志
        Log.i(TAG, "onCreate: ");
        etNumber = (EditText) findViewById(R.id.et_number);
        etPhone = (EditText) findViewById(R.id.et_phone);
        etNumber2 = (EditText) findViewById(R.id.et_number2);
        Button btnCall = (Button) findViewById(R.id.btn_call);
        Button btnSave = (Button) findViewById(R.id.btn_save);

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cutString = etNumber.getText().toString().trim();
                phoneNumber = etPhone.getText().toString().trim();
                phoneMap.put(cutString, phoneNumber);
            }
        });


        btnCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //隐式意图跳转到拨号界面
                Intent intent = new Intent();
                //跳转的Action
                intent.setAction(Intent.ACTION_CALL);
                //拨打的电话号码，需要从文本框中获取缩写号码
                String cutNumber = etNumber2.getText().toString().trim();//trim 去掉前后的空格
                //根据缩写号码找到完整号码
                String phoneNumber = phoneMap.get(cutNumber);

                intent.setData(Uri.parse("tel:" + phoneNumber));
                //跳转
                startActivity(intent);
            }
        });

    }

    private void requestMyAppPermission() {
        //申请权限
        String[] perms = {Manifest.permission.CALL_PHONE};
        if(!EasyPermissions.hasPermissions(getApplicationContext(), perms))
        {
            EasyPermissions.requestPermissions(this, "call_phone", 1, perms);
        }
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        //同意授权
        Log.i(TAG, "onPermissionsGranted: ");
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        //不同意授权
        Log.i(TAG, "onPermissionsDenied: ");

    }
}
